$(function () {
    $("#jqGrid").jqGrid({
        url: (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + 'sys/user/allot',
        datatype: "json",
        colModel: [			
			{ label: '用户名', name: 'username', width: 75 },
            { label: '所属部门', name: 'deptName', sortable: false, width: 75 },
			{ label: '邮箱', name: 'email', width: 90 },
			{ label: '手机号', name: 'mobile', width: 100 },
			{ label: '状态', name: 'status', width: 60, formatter: function(value, options, row){
				return value === 0 ? 
					'<span class="label label-danger">禁用</span>' : 
					'<span class="label label-success">正常</span>';
			}},
            {
                label: '用户类型', name: 'userType', width: 60, formatter: function (value, options, rpw) {
                    if (value === 1) {
                        return '管理员';
                    }
                    return '系统用户';
                }
            },
             { label: '创建时间', name: 'createTime', index: "create_time", width: 85}
        ],
		viewrecords: true,
        height:"100%",
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#myapp',
    data:{
        q:{
            username: null
        },
        showList: true,
        title:null,
        roleList:{},
        userIds:[],
        user:{
            status:1,
            // userType:0,
            deptId:null,
            deptName:null,
            roleIdList:[]
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        allotManager: function () {
            var userIds = getSelectedRows();
            if(userIds == null){
                return ;
            }

            confirm('确定要分配选中的记录？', function(){
                vm.packageAllot(userIds);
            });
        },
        packageAllot: function(userIds){

            $.ajax({
                type: "POST",
                url: (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/user/allotManager",
                contentType: "application/json",
                data: JSON.stringify(userIds),
                success: function(r){
                    if(r.code == 0){
                        toast('操作成功');
                        vm.reload();
                    }else{
                        warning(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'username': vm.q.username},
                page:page
            }).trigger("reloadGrid");
        },
        getUserTypeList: function () {
        	var url = "sys/dict/listByType/userType";
        	var $userType =$("#userType");
        	$userType.empty();
        	$.get((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + url,function (r) {
        		$.each(r.list,function (item,obj) {
                    var $option = '';
                    if(obj.code == '3'){
                         $option = $("<option value='"+obj.code+"' selected>"+obj.value+"</option>");
                    }else {
                         $option = $("<option value='"+obj.code+"'>"+obj.value+"</option>");
                    }
	        		$userType.append($option);
        		});
        	});       	
        }
    }
});